package tum0r.util;

import tum0r.model.StaticResource;

import java.util.LinkedList;

/**
 * 工程: ResourcesDatabase<br>
 * 包: tum0r.util<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/10/5 09:09<br>
 * <br>
 */
public class DealWithSearch {
    public String getSizeScale(String searchText) {
        StringBuilder result = new StringBuilder("=");
        if (!StringUtils.isNullOrOnlyWhiteSpace(searchText)) {
            int index = searchText.indexOf(":");
            if (index != -1 && (searchText.length() - 1) > index) {
                String temp = searchText.substring(index + 1);
                result = new StringBuilder();
                for (int i = 0; i < temp.length(); i++) {
                    char c = temp.charAt(i);
                    if (c == '>' || c == '<' || c == '=') {
                        result.append(temp.charAt(i));
                    } else {
                        break;
                    }
                }
            }
        }
        return result.toString();
    }

    public long getSize(String searchText) {
        long result = 0;
        if (!StringUtils.isNullOrOnlyWhiteSpace(searchText)) {
            searchText = searchText.toLowerCase();
            long x = 1;
            if (searchText.endsWith("gb") || searchText.endsWith("g")) {
                x = 1024 * 1024 * 1024;
                searchText = searchText.replace("gb", "").replace("g", "");
            } else if (searchText.endsWith("mb") || searchText.endsWith("m")) {
                x = 1024 * 1024;
                searchText = searchText.replace("mb", "").replace("m", "");
            } else if (searchText.endsWith("kb") || searchText.endsWith("k")) {
                x = 1024;
                searchText = searchText.replace("kb", "").replace("k", "");
            } else {
                searchText = searchText.replace("b", "");
            }
            boolean flag = true;
            int index = searchText.indexOf(":");
            StringBuilder x1 = new StringBuilder();
            if (index != -1 && (searchText.length() - 1) > index) {
                char[] temp = searchText.substring(index + 1).toCharArray();
                for (int i = 0; i < temp.length; i++) {
                    char c = temp[i];
                    if (c >= '0' && c <= '9') {
                        x1.append(c);
                    } else if (c != '>' && c != '<' && c != '=') {
                        flag = false;
                        break;
                    }
                }
            }
            if (flag) {
                result = x * Long.parseLong(x1.toString());
            }
        }
        return result;
    }

    public Object getSearchContent(String searchText) {
        return getSearchContent(searchText, null);
    }

    public Object getSearchContent(String searchText, Class<?> c) {
        c = c == null ? String.class : c;
        Object result = "%" + searchText + "%";
        if (!StringUtils.isNullOrOnlyWhiteSpace(searchText)) {
            int index = searchText.indexOf(":");
            if (index != -1 && (searchText.length() - 1) > index) {
                String temp = searchText.substring(index + 1);
                if (c != String.class) {
                    result = c.cast(temp);
                } else {
                    result = "%" + temp + "%";
                }
            }
        }
        return result;
    }


    public LinkedList<Object> dealWith(String searchText) {
        LinkedList<Object> result = new LinkedList<>();
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM ");
        sql.append(StaticResource.FILE_INFORMATION_TABLE);
        sql.append(" WHERE 1 = 1");
        if (!StringUtils.isNullOrOnlyWhiteSpace(searchText)) {
            String[] searchArray = searchText.trim().split(System.lineSeparator());
            int index = 0;
            for (String temp : searchArray) {
                String old = temp.trim();
                temp = old.toLowerCase();
                if (temp.startsWith("filename") || temp.startsWith("文件名") || temp.startsWith("名称")) {
                    sql.append(" AND (Name LIKE @p");
                    result.add(getSearchContent(old));
                } else if (temp.startsWith("filetype") || temp.startsWith("type") || temp.startsWith("文件类型") || temp.startsWith("类型") || temp.startsWith("后缀名")) {
                    sql.append(" AND (Type LIKE @p");
                    result.add(getSearchContent(old.toLowerCase()));
                } else if (temp.startsWith("createtime") || temp.startsWith("文件创建时间") || temp.startsWith("创建时间")) {
                    sql.append(" AND (CreateTime @p");
                    result.add(getSearchContent(old.toLowerCase()));
                } else if (temp.startsWith("lastaccesstime") || temp.startsWith("lasttime") || temp.startsWith("lastreadtime") || temp.startsWith("上次访问时间") || temp.startsWith("访问时间")) {
                    sql.append(" AND (LastAccessTime @p");
                    result.add(getSearchContent(old.toLowerCase()));
                } else if (temp.startsWith("filesize") || temp.startsWith("size") || temp.startsWith("文件大小") || temp.startsWith("大小")) {
                    sql.append(" AND (Size ");
                    sql.append(getSizeScale(old));
                    sql.append(" @p");
                    result.add(getSize(old));
                } else {
                    sql.append(" AND (Label LIKE @p");
                    result.add(getSearchContent(old));
                }
                sql.append(index++);
                sql.append(")");
            }
        }
        result.addFirst(sql.toString());
        return result;
    }
}
